package com.delos.dao.api;

import com.delos.api.cache.IUserCache;
import com.delos.api.dao.IUserDao;
import com.delos.api.entity.UserDO;
import com.delos.dao.mapper.UserMapper;
import com.delos.rpc.dto.UserDTO;
import org.redisson.api.RKeys;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 用户数据库操作
 *
 * @author zouyaowen
 * @date 2020-05-17
 */
@Repository
public class UserDao implements IUserDao {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IUserCache userCache;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Optional<UserDO> getUser(Long userId) {
        RList<Object> abc = redissonClient.getList("abc");
        if (userId == null) {
            return Optional.empty();
        }
        UserDO userDO = userMapper.selectById(userId);
        abc.add(userDO);
        return Optional.ofNullable(userDO);
    }

    @Override
    public void addUser(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO, userDO);
        userDO.setPassword("aaa");
        userMapper.insert(userDO);
    }

    @Override
    public Optional<UserDO> getUserCache(Long userId) {
        UserDO userDO = userCache.getUserCache(userId);
        return Optional.ofNullable(userDO);
    }

    @Override
    public void updateUserCache(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO,userDO);
        userCache.updateUserCache(userDO);
    }

    @Override
    public void deleteUserCache(Long userId) {
        userCache.deleteUserCache(userId);
    }
}
